<?php
include '/StatsProtocol.php'
include '/HtmlString.php'

    class SkyWalkerReader implements IReader
    {
        private $i = 1;

        public function getLinks()
        {
            $statsProtocol = new StatsProtocol();
            $htmlString = new HtmlString();
            $advs = array();
            $continueSearch = true;
            while ($continueSearch)
            {               
                for ($k = 2; $k < 12; $k++)
                {
                    $htmlCode = $statsProtocol->getHtmlCode("http://www.skywalker.gr/advertsMenu.aspx?sectorID=15&fromRow=". $this->i);
                    $SearchString = "ctl00_masterContent_advertsGridView_ctl0". $k ."_titleLinkButton";
                    if ($k >= 10)
                    {
                        $SearchString = "ctl00_masterContent_advertsGridView_ctl". $k ."_titleLinkButton";
                    }

                    $firstChr = strpos($htmlCode,$SearchString);
                    if ($firstChr == -1)
                    {
                        break;
                    }
                    $cuttedHtmlCode = substr($htmlCode,$firstChr);
                    $cleanedHtmlCode = $htmlString->cleanUrl($cuttedHtmlCode, "href="."\"", "\"".">", "sky");
                    $newUrl = "http://www.skywalker.gr/". $cleanedHtmlCode;
                    $advs[]=$newUrl;
                }
                $this->i = $this->i + 10;
                $htmlCode = $statsProtocol->getHtmlCode("http://www.skywalker.gr/advertsMenu.aspx?sectorID=15&fromRow=". $this->i);
                $continueSearch = $htmlString->pageExists($htmlCode, "advertPageLinkCurrentClass'>1<");
            }
            return $advs;
        }

        public function doTheWork() 
        {
            $statsProtocol = new StatsProtocol();
            $htmlString = new HtmlString();
            $advs = array();
            $advsResult= array();
            $reader = new SkyWalkerReader();
            $advs = $reader->getLinks();

            for ($j = 0; $j < count($advs); $j++)
            {
                $advertisements = new Advertisements();
                $htmlCode = $statsProtocol->getHtmlCode($advs['$j']);
                $cleanedhtmlCode = $htmlString->cleanUrl($htmlCode, "<ul>", "<tr><td valign=" . "\"" . "bottom" . "\"" . ">&nbsp;</td></tr>", "sky");
                $cleanedhtmlCode = $htmlString->StripTagsCharArray($cleanedhtmlCode);
                $advertisements->setContent($cleanedhtmlCode);
                $date = $htmlString->getDate($htmlCode, "Ημερομηνία δημοσίευσης/ανανέωσης:", "sky");
                $advertisements->setDate($date);
                $advsResult[]=$advertisements;
            }
            return $advsResult;
        }
    }
?>
